import configparser
import os
from enum import Enum, unique


# 此类主要负责conf的读取与配置
@unique
class Conf(Enum):
    MONGO_CONF = {'def_db': 'go-online', 'filePath': '/conf/mongo.conf'}
    MYSQLDB_CONF = {'def_db': 'mysql-test', 'filePath': '/conf/mysqldb.conf'}
    REDISDB_CONF = {'def_db': 'quota-online', 'filePath': '/conf/redisdb.conf'}

    # 取对应库的对应配置
    def getConfVal(self, sections, confName):
        return CONF_ALL[self.name].get(sections, confName)

    # 取默认库的配置
    def get(self, confName):
        return self.getConfVal(self.value['def_db'], confName)


def init_conf():
    CONF_ALL = {}
    for conf in Conf:
        config = configparser.ConfigParser()
        dbconfPath = os.path.abspath(os.path.dirname(__file__)) + conf.value['filePath']
        config.read(dbconfPath, encoding='UTF-8')
        CONF_ALL[conf.name] = config

    return CONF_ALL


CONF_ALL = init_conf()

if __name__ == '__main__':
    print(Conf.MONGO_CONF.getConfVal('go-online', "host"))
